package JDBC_Test.Student.Service;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Scanner;

import Test.StreamUtils.StreamUtils;
import JDBC_Test.Student.Dao.StudentDao;
import JDBC_Test.Student.Model.StudentModel;

/**
 * 
 * @author dell 学生档案管理
 */
public class Main {

	StudentDao studentDao = new StudentDao();
	// 保存学生
//	List<StudentModel> list =  new ArrayList<StudentModel>();

	public static void main(String[] args) throws IOException, SQLException {
		// TODO Auto-generated method stub

		Main m = new Main();

		m.print();
		Scanner scanner = new Scanner(System.in);
		while (true) {
			System.out.println("请输入操作：");
			String s2 = scanner.nextLine();
			int operation = Integer.valueOf(s2);
			if (operation == 0) {
				System.out.println("感谢使用，再见！");
				break;
			}
			while (operation != 0) {
				if (operation == 1) {
					m.selectAllStudent();
					break;
				} else if (operation == 2) {
					System.out.println("请输入要查找的学生姓名：");
					String nameString = scanner.nextLine();
					m.selectByName(nameString);
					break;
				} else if (operation == 3) {
					System.out.println("请输入要查找的专业：");
					String deptString = scanner.nextLine();
					m.selectByDept(deptString);
					break;
				} else if (operation == 4) {
					System.out.println("请输入存档学生数量");
					String s3 = scanner.nextLine();
					int nn = Integer.valueOf(s3);
					m.insertStudent(nn);
					break;
				} else if (operation == 5) {
					System.out.println("输入删除学生的学号：");
					String sno = scanner.nextLine();
					m.deleteById(sno);
					break;
				} else if (operation == 6) {
					System.out.println("请输入要修改学生的学号");
					String sno = scanner.nextLine();
					m.update(sno);
					break;
				}
			}
		}

	}

	public void print() {
		System.out.println("-----学生信息管理-----");
		System.out.println("输入1：查看所有学生信息");
		System.out.println("输入2：根据学生姓名查找学生");
		System.out.println("输入3：根据专业查找学生");
		System.out.println("输入4：录入学生信息");
		System.out.println("输入5：删除某个学生");
		System.out.println("输入6：修改学生信息");
		System.out.println("输入0：退出本系统");
	}

	/**
	 * 修改
	 * 
	 * @param sno
	 */
	private void update(String sno) {

		// TODO Auto-generated method stub
		Scanner scanner = new Scanner(System.in);
		System.out.println("输入修改后的姓名：");
		String name = scanner.nextLine();
		System.out.println("输入修改后的年龄：");
		String string = scanner.nextLine();
		Integer age = Integer.valueOf(string);
		System.out.println("输入修改后的专业：");
		String dept = scanner.nextLine();
		StudentModel studentModel = new StudentModel(name, age, null, sno, dept);
		studentDao.update(studentModel);
		System.out.println("修改完成");

	}

	/**
	 * 删除
	 * 
	 * @param sno
	 */
	private void deleteById(String sno) {
		// TODO Auto-generated method stub
		StudentModel studentModel = new StudentModel(null, null, null, sno, null);
		studentDao.delete(studentModel);
		System.out.println("删除成功");

	}

	/**
	 * 插入数据
	 * 
	 * @param num
	 * @throws IOException
	 * @throws SQLException
	 */
	public void insertStudent(int num) throws IOException, SQLException {
		Scanner scanner = new Scanner(System.in);
		System.out.println("请输入学生信息");
		for (int i = 0; i < num; i++) {
			System.out.print("姓名:");
			String name = scanner.nextLine();
			System.out.print("年龄:");
			String string = scanner.nextLine();
			int age = Integer.valueOf(string);
			System.out.print("性别:");
			String string2 = scanner.nextLine();
			int sex = Integer.valueOf(string2);
			System.out.print("学号:");
			String sno = scanner.nextLine();
			System.out.print("专业:");
			String dept = scanner.nextLine();
			StudentModel student = new StudentModel(name, age, sex, sno, dept);
			studentDao.insert(student);
//			list.add(student);	
		}
		System.out.println("录入信息成功");

	}

	/**
	 * 查询所有
	 */
	public void selectAllStudent() {
		System.out.println("当前学生信息:");
		StudentModel studentModel = new StudentModel();
		studentDao.selectList(studentModel);
	}

	/**
	 * 根据姓名查找
	 * 
	 * @param name
	 */
	public void selectByName(String name) {

		StudentModel studentModel = new StudentModel(name, null, null, null, null);
		System.out.println("该学生信息为：");
		studentDao.selectList(studentModel);
	}

	/**
	 * 根据专业查找
	 * 
	 * @param dept
	 */
	public void selectByDept(String dept) {

		StudentModel studentModel = new StudentModel(null, null, null, null, dept);
		System.out.println("该专业学生信息：");
		studentDao.selectList(studentModel);
	}

}
